//
//  WQ_accessToken.swift
//  xinlang
//
//  Created by 联创—王增辉 on 2019/3/26.
//  Copyright © 2019年 lcWorld. All rights reserved.
//

import UIKit

private let userModelPath = "userModelPath"

class WQ_accessTokenModel: NSObject,Codable {
    
    @objc var access_token:String?
    @objc var isRealName:String?
    @objc var uid:String?
    @objc var remind_in:String?
    @objc var expireDate:Date?
    
    @objc var expires_in:TimeInterval = 0 {
        didSet{
            self.expireDate = Date(timeIntervalSinceNow: expires_in)
        }
    }
    //MARK: - 保存数据
    func save()  {
        
        guard let model = try? JSONEncoder().encode(self) else{
            return
        }
        (model as NSData).write(toFile: String.locationPath(path: userModelPath), atomically: true)
    }
    func remove() {
        access_token = nil
        isRealName = nil
        uid = nil
        remind_in = nil
        expireDate = nil
        let manager = FileManager.default
        try? manager.removeItem(atPath: String.locationPath(path: userModelPath))
    }
    static let accessTokenModel: WQ_accessTokenModel = {
        var Model = WQ_accessTokenModel()
        guard let data:Data = NSData(contentsOfFile: String.locationPath(path: userModelPath)) as Data?,let model = try? JSONDecoder().decode(WQ_accessTokenModel.self, from: data) else{
            return Model
        }
        Model = model
        return Model
    }()
}
